package com.xunchi.haichuan.user.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.user.service.UserService;
import com.xunchi.haichuan.user.service.UserServiceImpl;
import com.xunchi.haichuan.utility.Digest;

/**
 * 作者：kongxb<br>
 * 模块名称：用户模块<br>
 * 功能描述：用户注册<br>
 * 维护记录：2009.12.3, kongxb 创建该类
 */
public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 6209022484834904698L;

	private static Log logger = LogFactory.getLog(RegisterAction.class);

	private transient String loginName;
	private transient String password;
	private transient String password2;
	private transient String userName;
	private transient String email;

	private boolean success = true;
	private String msg = "";

	private UserService userService;

	public String execute() throws Exception {
		return Action.SUCCESS;
	}

	/**
	 * 添加用户
	 * 
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception {
		logger.debug("Begin addUser()");
		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl(), true);
		userService.addUser(loginName, Digest.getSHA1Digest(password), userName, email, 0, 0);
		return Action.SUCCESS;
	}

	/**
	 * 校验用户注册信息
	 */
	public String validateLoginName() throws Exception {
		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl());
		if (loginName == null || !loginName.matches("^[A-Za-z][A-Za-z0-9_-]*$")) {
			success = false;
			msg = this.getText("USER.error.register.loginName.illegal");
		} else if (userService.checkLoginName(loginName)) {
			success = false;
			msg = this.getText("USER.error.register.loginName.duplicate");
		}
		return Action.SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
